package org.example.discovery;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.example.common.ResultData;
import org.example.common.ServiceConfig;
import org.example.common.ServiceConfigParam;
import org.example.config.DiscoveryProperties;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sxt
 * @version 1.0
 * @description 服务注册与发现类
 * @date 2023/8/3 22:00
 */
@SuppressWarnings("unchecked")
@Data
public class Discovery implements IServiceDiscovery {

    /**
     * 服务本身配置信息
     */
    private ServiceConfig serviceConfig;

    /**
     * 依赖其他服务 其他服务的配置信息集合 cache
     */
    private Map<String, ServiceConfig> targetServiceConfig;

    @Resource
    DiscoveryProperties discoveryProperties;

    @Resource
    RestTemplate restTemplate;


    @Override
    public List<ServiceConfig> discoveryService(ServiceConfigParam serviceConfigParam) {
        String url = discoveryProperties.getProtocol().getProtocol() +
                discoveryProperties.getIp() +
                ":" + discoveryProperties.getPort() +
                "/" + discoveryProperties.getApi()
                + "/discovery";
        HashMap<String, Object> param = new HashMap<>();
        ResultData<List<ServiceConfig>> result = restTemplate.getForObject(url, ResultData.class, param);
        // 得到一个用于发送http请求的对象
        return result != null ? result.getObj() : null;
    }


    @Override
    public boolean registryService(ServiceConfigParam serviceConfigParam) {
        String url = discoveryProperties.getProtocol().getProtocol() +
                discoveryProperties.getIp() +
                ":" + discoveryProperties.getPort() +
                "/" + discoveryProperties.getApi()
                + "/registry";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/json");
        HttpEntity<String> httpEntity = new HttpEntity<>(JSONObject.toJSONString(serviceConfigParam), httpHeaders);
        ResultData<Boolean> resultData = restTemplate.postForObject(url, httpEntity, ResultData.class);
        return resultData != null ? resultData.getObj() : false;
    }

    @Override
    public boolean unRegistryService(ServiceConfigParam serviceConfigParam) {
        String url = discoveryProperties.getProtocol().getProtocol() +
                discoveryProperties.getIp() +
                ":" + discoveryProperties.getPort() +
                "/" + discoveryProperties.getApi()
                + "/unRegistry";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/json");
        HttpEntity<String> httpEntity = new HttpEntity<>(JSONObject.toJSONString(serviceConfigParam), httpHeaders);
        ResultData<Boolean> resultData = restTemplate.exchange(url, HttpMethod.DELETE, httpEntity, ResultData.class).getBody();
        return resultData != null ? resultData.getObj() : false;
    }

}
